import store from '@/store/index'
import {Encrypt} from "@/untils/aes";
import $http from "@/api/fetch";
import api from '@/api/api';
import {routes, ansycRoutes} from '@/router/routes'
import work from '@/components/work.vue'

/*
 *
 * @param {异步路由表} ansycRoutes
 * @param {数据库权限拉取} perms
 */


/**
 * 递归处理角色权限
 */
function getansycRoutes(tmpRoutes, perms) {
    let aa = perms.split(',');
    var result = tmpRoutes.filter(route => {
            if (aa.indexOf(route.path) != -1) {
                if (route.children != undefined) {
                    let cc = aa.join(',');
                    route.children = getansycRoutes(route.children, cc);
                }
                return true;
            }
            return false;
        }
    )
    return result

}

const perm = {
    state: {
        routers: routes,
        addRouters: [],
        perms: false,//判断是否刷新过页面,
        permsList: null,
        _site_id_param: '',//站点id
        siteItems: [],
        isMasterSite: true,
        baseUrl: ''
    },
    mutations: {

        SET_ROUTERS: (state, obj) => {
            state.routers = routes.concat(obj.asRouters);//设置路由表
            state.addRouters = obj.asRouters;//动态路由表
            state.perms = true;//登录状态
            state.permsList = obj.permsList;//权限字符串
            state._site_id_param = obj.siteId;//站点id
            state.siteItems = obj._site_id_param;
            state.isMasterSite = obj.isMasterSite;
            state.baseUrl = obj.baseUrl
            let arr = [];
            for (let i in obj._site_id_param) {//站点id 数组
                arr.push(obj._site_id_param[i].id);
            }
            let local_site_id_param = localStorage.getItem('_site_id_param');
            if (local_site_id_param) {
                let isBeing = arr.indexOf(parseInt(local_site_id_param))
                if (isBeing >= 0) {

                    localStorage.setItem('_site_id_param', local_site_id_param);
                } else {

                    localStorage.setItem('_site_id_param', state._site_id_param);
                }
            } else {

                localStorage.setItem('_site_id_param', state._site_id_param);
            }

        },
        CLEAR_ROUTERS: (state) => {
            state.routers = routes;
            state.addRouters = [];
            state.perms = false;
            state.permsList = null;
            state._site_id_param = null;
            localStorage.setItem('sessionKey', '');
            localStorage.setItem('userName', '');
            window.location.reload();//此处退出vuex状态不刷新，目前发现重载页面可以处理。。
        }
    },
    actions: {
        setRouters({commit}) {
            return new Promise((resolve, reject) => {//动态加载路由权限
                $http.post(api.getPerms)
                    .then(res => {
                        let asRouters;
                        if (res.body.perms == '*') {
                            asRouters = ansycRoutes;
                        } else {
                            asRouters = getansycRoutes(ansycRoutes, res.body.perms);//递归过滤
//此处应该把 404 页面加进入
//将404 页面 全部重定向至 /work 免得用户看到404 界面 懵逼
                            asRouters.push({
                                path: '*',
                                name: '404',
                                redirect: '/work',
                                meta: {
                                    role: 'index'
                                },
                                hidden: true
                            })
                        }
                        commit('SET_ROUTERS', {
                            asRouters: asRouters,
                            permsList: res.body.perms,
                            _site_id_param: res.body.sites,
                            siteId: res.body.siteId,
                            isMasterSite: res.body.isMasterSite,
                            baseUrl: res.body.url
                        });
                        resolve(res);
                    }).catch(error => {
                    reject(error);
                })
            })
        },
        loginOut({commit}) {
            return new Promise((resolve, reject) => {
                $http.post(api.loginOut).then(res => {
                    if (res.code == '200') {
                        commit('CLEAR_ROUTERS');
                    }
                    resolve(res);
                }).catch(error => {
                    reject(error);
                })
            })
        }
    }

}

export default perm